package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.logging.LogException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;



    @RequestMapping("/input")
    @RequiredPermission(name="员工新增或修改",expression = "employee:input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles",roles);

        if (id!=null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);

             List<Role> myRoles = roleService.queryByRoleIds(employee.getId());
             model.addAttribute("myRoles",myRoles);

        }
        return "employee/input";
    }
    @RequestMapping("/list")
    @RequiredPermission(name="员工查询",expression = "employee:list")
    public String list(Model model,  EmployeeQueryObject qo){
//        int i= 1/0;
//        throw new LogicException("xxx");
         PageInfo<Employee> pageInfo = employeeService.query(qo);
         model.addAttribute("pageInfo",pageInfo);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        return "employee/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name="员工删除",expression = "employee:delete")
    public String delete(Long id){
        if (id!=null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list";  //让浏览器重新发一次请求
    }
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="员工新增或修改",expression = "employee:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpDate(Employee employee, Long[] roleIds){
        try {
            if (employee.getId()==null) {
                employeeService.save(employee,roleIds);
            }else{
                employeeService.update(employee,roleIds);
            }
            return new JsonResult(true,"编辑成功");
        }catch (Exception e){
            return new JsonResult(false,e.getMessage());
        }
    }

@ResponseBody
@RequestMapping("/checkUserName")
    public Map<String,Boolean> checkUserName(String username){
        Map<String, Boolean> data = new HashMap<>();
        data.put("valid",employeeService.checkUserName(username));
        return data;
    }
    @RequestMapping("/exportXls")
    public void export(HttpServletResponse response) throws IOException {
    response.setHeader("Content-Disposition","attachment;filename=employee.xls");
    employeeService.export().write(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        Workbook wb = WorkbookFactory.create(file.getInputStream());
        employeeService.improtXls(wb);
        return "redirect:/employee/list";
    }


    }






